package se.perp.c4.ui;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sounds {

    public enum Which {

        NOTIFICATION, GANDALF_WHITE, RECALL, PALANTIR, SHADOWFAX,
        LOST_ROUND, WON_ROUND, LOST_GAME, WON_GAME
    }
    private static Map<Which, String> streams = new HashMap<Which, String>();


    static {
        streams.put(Which.NOTIFICATION, "/sound/notification.wav");
        streams.put(Which.PALANTIR, "/sound/palantir.wav");
        streams.put(Which.RECALL, "/sound/recall.wav");
        streams.put(Which.GANDALF_WHITE, "/sound/gandalf.wav");
        streams.put(Which.SHADOWFAX, "/sound/shadowfax.wav");

        streams.put(Which.LOST_ROUND, "/sound/lostround.wav");
        streams.put(Which.LOST_GAME, "/sound/lostgame.wav");
        streams.put(Which.WON_ROUND, "/sound/wonround.wav");
        streams.put(Which.WON_GAME, "/sound/wongame.wav");

    }

    public static void play(Which which) {

        AudioInputStream ain = null;
        try {
            ain = AudioSystem.getAudioInputStream(
                    Sounds.class.getResourceAsStream(streams.get(which)));
            DataLine.Info info = new DataLine.Info(Clip.class, ain.getFormat());
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(ain);
            clip.start();
        } catch (LineUnavailableException ex) {
            Logger.getLogger(Sounds.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(Sounds.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Sounds.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                ain.close();
            } catch (IOException ex) {
                Logger.getLogger(Sounds.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}